package com.example.myapplication.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.example.myapplication.R;
import com.example.myapplication.base.App;
import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.http.BaseCallBack;
import com.example.myapplication.http.HttpHelp;

public class LoginActivity extends BaseActivity {

    private AppCompatTextView denglu;
    private AppCompatTextView zhuce;
    private AppCompatEditText zhanghao;
    private AppCompatEditText mima;
    private View zhanghaoView,mimaView;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);

        String token =App.get().getSharedPreferences().getString("token","");
        if(!token.isEmpty()){
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }
    }

    @Override
    public void initView() {
        denglu = findViewById(R.id.denglu);
        zhanghao = findViewById(R.id.zhanghao);
        mima = findViewById(R.id.mima);
        zhuce = findViewById(R.id.zhuce);
        zhanghaoView = findViewById(R.id.zhanghaoView);
        mimaView = findViewById(R.id.mimaView);

    }

    @Override
    public void initEvent() {
        zhanghao.setOnFocusChangeListener((view,b)->{
            if(b){
                zhanghaoView.setBackgroundResource(R.color.denglujian);
                mimaView.setBackgroundResource(R.color.textHint);
            }
        });
        mima.setOnFocusChangeListener((view,b)->{
            if(b){
                zhanghaoView.setBackgroundResource(R.color.textHint);
                mimaView.setBackgroundResource(R.color.denglujian);
            }
        });
        denglu.setOnClickListener(view -> {
            String mobile=zhanghao.getText().toString();
            String password=mima.getText().toString();
            if(mobile.isEmpty()){
                Toast.makeText(this,"请输入手机号",Toast.LENGTH_SHORT).show();
                return;
            }
            if(password.isEmpty()){
                Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();;
                return;
            }
            denglu.setEnabled(false);
            denglu.setText("正在登陆...");

            HttpHelp.get().ready("base","login")
                    .add("mobile",mobile)
                    .add("password",password)
                    .get(new BaseCallBack(){

                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            denglu.setEnabled(true);
                            denglu.setText("登陆");

                            App.get().getEditor().putString("token",data);
                            App.get().getEditor().commit();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        }

                        @Override
                        public void onFailure(String reason) {
                            Toast.makeText(LoginActivity.this,reason,Toast.LENGTH_SHORT).show();
                            denglu.setEnabled(true);
                            denglu.setText("登陆");
                        }
                    });
        });
        zhuce.setOnClickListener(v -> {
            Intent intent =new Intent(LoginActivity.this, RegisterActivity.class);
            startActivityForResult(intent,999);
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("onDestroy");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==999&&requestCode==999){
            zhanghao.setText(data.getStringExtra("zhanghao"));
        }
    }
}